package com.qububu.play.dineTogether.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.DinePeopleDao;
import com.qububu.play.dineTogether.dto.ActionPeopleDto;
import com.qububu.play.dineTogether.service.DinePeopleService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Service
public class DinePeopleServiceImpl implements DinePeopleService {

	@Resource
	private DinePeopleDao dinePeopleDao;
	
	@Resource
	private Cache dinePeopleCache;

	public List<ActionPeopleDto> getActionPeopleListByActionId(int actionId) {
		/*List<ActionPeopleDto> peopleList = (List<ActionPeopleDto>) getElementFromCache(actionId);
		if (peopleList == null) {
			peopleList = dinePeopleDao.getActionPeopleListByActionId(actionId);
			doSaveElemToCache(actionId, peopleList);
		}*/
		
		List<ActionPeopleDto> peopleList = dinePeopleDao.getActionPeopleListByActionId(actionId);
		return peopleList;
	}

	public boolean doAddActionPeople(ActionPeopleDto actionPeople) {
		/*int key = ParamConverter.convertToInteger(SystemUtil.decrypt(actionPeople.getActionId()));
		if (removeElementFromCache(key)) {
			dinePeopleDao.doAddActionPeople(actionPeople);
			return true;
		}
		return false;*/
		int peopleId = ParamConverter.convertToInteger(SystemUtil.decrypt(actionPeople.getPeopleId()));
		if (dinePeopleDao.getActionPeopleInfoByPeopleId(peopleId) == null) {

			dinePeopleDao.doAddActionPeople(actionPeople);
			return true;
		}
		
		return false;
	}

	public boolean doDeleteActionPeopleByActionId(int peopleId) {
		/*if (clearOfCache()) {
			return dinePeopleDao.doDeleteActionPeopleByPeopleId(peopleId);
		}
		return false;*/
		
		return dinePeopleDao.doDeleteActionPeopleByPeopleId(peopleId);
	}

	public boolean doChangePeopleStaff(int peopleId, int staffId) {
		/*if (clearOfCache()) {
			dinePeopleDao.changePeopleStaff(peopleId, staffId);
			return true;
		}
		return false;*/
		
		dinePeopleDao.changePeopleStaff(peopleId, staffId);
		return true;
	}
	
	private Object getElementFromCache(int key) {
		Element cacheElem = dinePeopleCache.get(key);

		if (cacheElem != null) {
//			System.out.println("\n\n--------调试--[dinePeople]--[从缓存中[取出]一个元素]-------\n\n");
			return cacheElem.getObjectValue();
		}
		return null;
	}
	
	private void doSaveElemToCache(int key, Object elem) {
		Element foodElement = dinePeopleCache.get(key);
		if (foodElement == null) {
			dinePeopleCache.put(new Element(key, elem));
//			System.out.println("\n\n--------调试--[dinePeopleCache]--[从缓存中[存入]一个元素]-------\n\n");
		}
	}

	// 清除指定key的缓存数据
	private boolean removeElementFromCache(int key) {
		if (dinePeopleCache.get(key) != null) {
//			System.out.println("\n\n--------调试--[dinePeopleCache]--[从缓存中[移除]一个元素]-------\n\n");
			return dinePeopleCache.remove(key);	
		}
		return true;
	}
	
	/**
	 * 清空缓存中所有数据
	 * */
	private boolean clearOfCache() {
		if (dinePeopleCache.getSize() > 0) {
			dinePeopleCache.removeAll();
//			System.out.println("\n\n--------调试--[dinePeopleCache]--[清空缓存数据]-------\n\n");
		}
		return true;
	}
}
